import * as signalR from '@aspnet/signalr'
import { getToken } from '@/utils/auth.js'
export default {
  SR: {},
  // 初始化连接
  initSR: function () {
    const that = this
    const token = getToken()
    const url = public_config.labor_api
    // let url = ''
    // console.log('token',token)
    // 1.获取系统中定义的baseURL
    // 2.初始化连接
    that.SR = new signalR.HubConnectionBuilder()
      .withUrl(url + '/signalr-hubs/alarm', {
        accessTokenFactory: () => encodeURI(token)
      })
      .configureLogging(signalR.LogLevel.Information)
      .build()
    // 3.携带参数
    // that.SR.qs = {
    //     enc_auth_token: encodeURI(token),
    // };
    // 4.启动连接的方法
    async function start() {
      try {
        await that.SR.start()
        console.log('signaR连接成功')
      } catch (err) {
        // console.log("err", err);
        setTimeout(start, 5000)
      }
    }
    // 5.关闭之后重连
    that.SR.onclose(async () => {
      await start()
    })
    // 6.启动连接
    start()
  },
  // 停止连接（这个方法好像没啥用，先放着吧）
  stopSR: function () {
    const that = this
    async function stop() {
      try {
        await that.SR.stop()
        console.log('signaR退出成功')
      } catch (err) {}
    }
    stop()
  }
}
